//
//  SDMessageListViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDMessageListViewController.h"
#import "SDMessageListData.h"
#import "SDMessageItemCell.h"

@interface SDMessageListViewController()<SDBaseDataDelegate,SDTableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)SDMessageListData* messageListData;
@end
@implementation SDMessageListViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initViews];
    
    _messageListData = [[SDMessageListData alloc] init];
    _messageListData.delegate = self;
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)loadData
{
    [_messageListData refreshData];
}

- (void)initViews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"系统消息"];
        UIImage* addImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"actionbar_btn_back.png");
        [self.navBar customNaviLeftButton:self action:@selector(onBack:) image:addImg];
       // [self.navBar customNaviRightButton:self action:@selector(onStore:) title:@"对比"];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showLoadingMoreAlways = YES;
    [self.tableView setMoreTipString:@"查看更多历史消息"];
}

- (BOOL)navigationBarHidden
{
    return NO;
}

- (void)onBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onStore:(id)sender
{
    
}

- (BOOL)isDataEmpty
{
    return CHECK_VALID_ARRAY(_messageListData.messageList)?NO:YES;
}

- (void)showEmpty
{
    UIImage *emptyImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle,@"blank_massage_none.png");
    SDBlankPageView* emptyView = [SDBlankPageView showBlankPageViewTo:self.view];
    [emptyView setImage:emptyImg withTitle:@"暂无消息" withSubTitle:@""];
}

- (void)removeEmpty
{
    [SDBlankPageView hideBlankPageErrorFrom:self.view];
}

#pragma mark VPTableView
- (void)tableViewShouldRefreshData:(SDTableView*)tableView
{
    [_messageListData refreshData];
}

- (void)tableViewShouldLoadMoreData:(SDTableView*)tableView
{
    [_messageListData loadMoreData];
}

- (BOOL)tableViewDataIsLoading:(SDTableView*)tableView
{
    return [_messageListData isLoading];
}

- (BOOL)tableViewHasMoreData:(SDTableView*)tableView
{
  //  return  !_messageListData.isOver;
    return NO;
}

- (NSDate*)tableViewDataLastUpdateTime:(SDTableView*)tableView
{
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (CHECK_VALID_ARRAY(_messageListData.messageList))
        return [_messageListData.messageList count];
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (CHECK_VALID_ARRAY(_messageListData.messageList))
        return kMessageCellHeight;
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    
    SDMessageItem* messageItem = [_messageListData.messageList objectAtIndex:row];
    SDMessageItemCell* cell = nil;
    static NSString* identifier = @"messageItemCell";
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if( cell == nil )
    {
        cell = [[SDMessageItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell setCellData:messageItem];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


#pragma mark -SDBaseDataDelegate
- (void)dataWillStartLoad:(SDBaseData*)model
{
    
}

- (void)data:(SDBaseData*)model didFinishLoadWithError:(NSError*)error nextDataType:(SDBaseDataType)nextDataType
{
    if (!error) {
        if ([model isKindOfClass:[SDMessageListData class]]) {
            [self.tableView reloadData];
            
            [self finishLoading];
        }
    } else {
        if ([model isKindOfClass:[SDMessageListData class]])
        {
            [self failedLoading];
        }
    }
}
@end
